package com.hongkzh.www.shop.service;

import com.google.common.collect.Lists;
import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.BigNumberFormat;
import com.hongkzh.www.conmon.DateUtil;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ProductService extends BaseService {
    public Page<Record> getCategoryProductList(String categoryId, int pageNumber, String sortId, String sortValue) {

        Kv cond = Kv.by("categoryId", categoryId).set("order", " order by " + sortId + " " + sortValue);
        SqlPara sqlPara = Db.getSqlPara("shop.getCategoryProductList", cond);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    /**
     * 新品列表
     *
     * @param pageNumber
     * @param sortId
     * @param sortValue
     * @return
     */
    public Page<Record> getNewProductList(int pageNumber, String sortId, String sortValue) {
        Kv cond = Kv.by("isNew", "1").set("order", " order by " + sortId + " " + sortValue);
        SqlPara sqlPara = Db.getSqlPara("shop.getNewProductList", cond);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    /**
     * 专题活动
     *
     * @return
     */
    public List<Record> getSpecialsList() {
        List<Record> records = Db.find(Db.getSql("shop.getSpecialsList"));
        for (Record r : records
        ) {
            doFilePath(r);
        }
        return records.size() > 0 ? records : null;
    }

    /**
     * 爆款
     *
     * @return
     */
    public List<Record> getLuckyBurstsList() {
        List<Record> records = Db.find(Db.getSql("shop.getLuckyBurstsList"));
        for (Record r : records
        ) {
            doFilePath(r);
        }
        return records.size() > 0 ? records : null;
    }

    /**
     * 秒杀
     *
     * @return
     */
    public List<Record> getSeckillsList() {
        List<Record> records = Db.find(Db.getSql("shop.getSeckillsList"));
        int currentHour, beginDate, endDate;
        for (Record r : records
        ) {
            currentHour = r.getInt("currentHour");
            beginDate = r.getInt("beginDate");
            endDate = r.getInt("endDate");
            r.set("sortDate", currentHour - beginDate);
            if (currentHour >= beginDate && currentHour < endDate) {
                r.set("sortDate", 0);
            } else {
                if (currentHour - beginDate > 0) {
                    r.set("sortDate", currentHour - beginDate - 24);
                }
            }
            doFilePath(r);
        }
        return records.size() > 0 ? records : null;
    }

    /**
     * 折扣
     *
     * @return
     */
    public List<Record> getdDiscountsList() {
        List<Record> records = Db.find(Db.getSql("shop.getdDiscountsList"));
        for (Record r : records
        ) {

            doFilePath(r);
        }
        return records.size() > 0 ? records : null;
    }

    /**
     * 分类
     *
     * @return
     */
    public List<Record> getCategorysList() {

        List<Record> categorys = Db.find(Db.getSql("shop.getRecProductCategory"));
        Record r2 = new Record();
        r2.set("categoryId", "-1");
        r2.set("categoryName", "更多分类");
        r2.set("imgSrc", "/userfiles/1/images/shop/hkShopCategory/2018/09/sy_gd.png");
        categorys.add(r2);
        for (Record r : categorys
        ) {
           /* List<Record> lists=Db.find(Db.getSqlPara("shop.getRecProductCategoryList",r.getStr("categoryId")));
            for (Record l:lists
                    ) {
                doFilePath(l);
            }*/
            doFilePath(r);
            /*  r.set("products",lists);*/
        }
        return categorys.size() > 0 ? categorys : null;
    }

    /**
     * 新品推荐
     *
     * @return
     */
    public List<Record> getNewRecommendsList() {
        List<Record> records = Db.find(Db.getSql("shop.getNewRecommendsList"));
        for (Record r : records
        ) {
            doFilePath(r);
        }
        return records.size() > 0 ? records : null;

    }

    /**
     * 折扣分页
     *
     * @param pageNumber
     * @return
     */
    public Page<Record> getdDiscountsList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("shop.getdDiscountsALLList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()
        ) {

            doFilePath(r);
        }
        return records;
    }

    /**
     * 爆款列表
     *
     * @param pageNumber
     * @return
     */
    public Page<Record> getLuckyBurstALLList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("shop.getLuckyBurstALLList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);
        List<Record> users = null;
        for (Record r : records.getList()
        ) {
            users = Db.find(Db.getSqlPara("shop.getUsersByProductId", r.getStr("productId")));
            for (Record u : users
            ) {
                doImgPath(u);
                doName(u);
            }
            doFilePath(r);
            r.set("users", users);

        }
        return records;
    }

    /**
     * 秒杀
     *
     * @param pageNumber
     * @return
     */
    public Page<Record> getSeckillALLList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("shop.getSeckillALLList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);
        List<Record> users = null;
        int currentHour = 0, beginDate = 0, endDate = 0;
        String currentTime = "";
        for (Record r : records.getList()
        ) {
            users = Db.find(Db.getSqlPara("shop.getUsersByProductId", r.getStr("productId")));
            for (Record u : users
            ) {
                doFilePath(u);
                doName(u);
            }
            doFilePath(r);
            r.set("users", users);


            if (null != records && null != records.getList() && records.getList().size() > 0) {
                currentHour = r.getInt("currentHour");
                beginDate = r.getInt("beginDate");
                endDate = r.getInt("endDate");
                currentTime = r.getStr("currentTime");
            }
            r.set("currentTime", currentTime);
            r.set("beginDate", beginDate);
            r.set("endDate", endDate);
            r.set("currentHour", currentHour);
            r.set("sortDate", currentHour - beginDate);
            if (currentHour >= beginDate && currentHour < endDate) {
                r.set("sortDate", 0);
            } else {
                if (currentHour - beginDate > 0) {
                    r.set("sortDate", currentHour - beginDate - 24);
                }
            }
        }
        return records;

    }

    public Record getProductSkuBySku(String skuId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductSkuBySku", skuId);
        return Db.findFirst(sqlPara);

    }

    public List<Record> getProductSkuImagesBySku(String skuId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductSkuImagesBySku", skuId);
        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            doImgPath(r);
        }
        return list.size() > 0 ? list : null;
    }

    public Page<Record> getProductCommentById(String productId, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductCommentById", productId);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);
        List<Record> images = null;
        for (Record r : records.getList()
        ) {
            r.set("images", null);
            r.set("createDate", DateUtil.FormatDate(r.getDate("createDate")));
            r.set("orderDate", DateUtil.FormatDate(r.getDate("orderDate")));
            images = Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId", r.getStr("commentId")));
            for (Record u : images
            ) {
                doFilePath(u);
            }
            doFilePath(r);
            if (null != images && images.size() > 0) {
                r.set("images", images);
            }

        }
        return records;
    }

    public Record getProductById(String productId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductById", productId);
        Record record = Db.findFirst(sqlPara);
        int orders = Db.findFirst(Db.getSqlPara("shop.getProductOrdersById", productId)).getInt("orders");
        record.set("orders", orders);
        record.set("lb", 0);
        sqlPara = Db.getSqlPara("shop.getProductImagesById", productId);
        List<Record> images = Db.find(sqlPara);
        for (Record u : images
        ) {
            doImgPath(u);
        }
        sqlPara = Db.getSqlPara("shop.getShopById", record.getStr("shopId"));
        Record shop = Db.findFirst(sqlPara);
        doImgPath(shop);
        record.set("shop", shop);
        record.set("images", images);

        sqlPara = Db.getSqlPara("shop.getCouponsById", record.getStr("productId"));
        List<Record> coupons = Db.find(sqlPara);
        record.set("coupons", coupons);

        sqlPara = Db.getSqlPara("shop.getProductColorsById", productId);
        List<Record> colors = Db.find(sqlPara);
        record.set("colors", colors);
        String descript = record.getStr("descript");
        descript = descript.replaceAll("/userfiles/", PropKit.get("fileServer") + "/userfiles/");
        record.set("descript", descript);

        sqlPara = Db.getSqlPara("shop.getProductSpecsById", productId);
        List<Record> specs = Db.find(sqlPara);
        record.set("specs", specs);


        sqlPara = Db.getSqlPara("shop.getProductSkusById", productId);
        List<Record> skus = Db.find(sqlPara);
        record.set("skus", skus);

        sqlPara = Db.getSqlPara("shop.getProductCommentById", productId);
        Record comment = Db.findFirst(sqlPara);

        if (null != comment) {
            images = Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId", comment.getStr("commentId")));
            for (Record u : images
            ) {
                doImgPath(u);
            }
            doFilePath(comment);
            comment.set("images", null);
            if (null != images && images.size() > 0) {
                comment.set("images", images);
            }
        }
        record.set("comment", comment);


        sqlPara = Db.getSqlPara("shop.getProductRecsById", productId);
        List<Record> recs = Db.find(sqlPara);
        for (Record u : recs
        ) {
            doImgPath(u);
        }
        record.set("recs", recs);

        //运费
        sqlPara = Db.getSqlPara("shop.getFreightById", productId);
        Record freRecs = Db.findFirst(sqlPara);

        List<Record> subFreRecsList = Lists.newArrayList();
        //子运费
        if (freRecs != null) {
            sqlPara = Db.getSqlPara("shop.getSubFreightById", freRecs.getStr("freightId"));
            subFreRecsList = Db.find(sqlPara);
            freRecs.set("subFreRecsList", subFreRecsList);
        }

        //运费省名称
        if (freRecs != null) {
            StringBuilder sb = new StringBuilder();
            String[] freStr = freRecs.getStr("provinceId").split(",");
            for (String str : freStr) {
                Record rsd = Db.findFirst(Db.getSqlPara("shop.getProviceNameById", str));
                if (StrKit.notNull(rsd)) {
                    sb.append(rsd.get("name").toString());
                    sb.append(",");
                }
            }
            freRecs.set("proviceName", sb.toString());
        }

        //子运费省名称
        if (subFreRecsList.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (Record re : subFreRecsList) {
                String[] subFreStr = re.getStr("provinceId").split(",");
                for (String str : subFreStr) {
                    Record rsd = Db.findFirst(Db.getSqlPara("shop.getProviceNameById", str));
                    if (StrKit.notNull(rsd)) {
                        sb.append(rsd.get("name").toString());
                        sb.append(",");
                    }
                }
                re.set("subProviceName", sb.toString());
                sb.setLength(0);
            }
        }

        record.set("freRecs", freRecs);

        return record;
    }


    public Record getProductDiscountsById(String productId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductById", productId);
        Record record = Db.findFirst(sqlPara);


        sqlPara = Db.getSqlPara("shop.getDiscountsLastStockById", productId);
        Record discount = Db.findFirst(sqlPara);
        if (discount != null) {

            record.set("activityPrice", discount.get("activityPrice"));
            record.set("lastStocks", discount.get("lastStocks"));
            record.set("currentTime", discount.get("currentTime"));
            record.set("beginDate", discount.get("beginDate"));
            record.set("endDate", discount.get("endDate"));
            record.set("discount", discount.get("discount"));
            record.set("people", "300");
            record.set("currentHour", discount.get("currentHour"));
        }

        sqlPara = Db.getSqlPara("shop.getProductImagesById", productId);
        List<Record> images = Db.find(sqlPara);
        for (Record u : images
        ) {
            doImgPath(u);
        }

        record.set("images", images);


        sqlPara = Db.getSqlPara("shop.getProductColorsById", productId);
        List<Record> colors = Db.find(sqlPara);
        record.set("colors", colors);


        sqlPara = Db.getSqlPara("shop.getProductSpecsById", productId);
        List<Record> specs = Db.find(sqlPara);
        record.set("specs", specs);


        sqlPara = Db.getSqlPara("shop.getProductSkusById", productId);
        List<Record> skus = Db.find(sqlPara);
        record.set("skus", skus);

        sqlPara = Db.getSqlPara("shop.getProductCommentById", productId);
        Record comment = Db.findFirst(sqlPara);

        if (null != comment) {
            images = Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId", comment.getStr("commentId")));
            for (Record u : images
            ) {
                doImgPath(u);
            }
            doFilePath(comment);
            comment.set("images", null);
            if (null != images && images.size() > 0) {
                comment.set("images", images);
            }

        }
        record.set("comment", comment);


        sqlPara = Db.getSqlPara("shop.getProductRecsById", productId);
        List<Record> recs = Db.find(sqlPara);
        for (Record u : recs
        ) {
            doImgPath(u);
        }
        record.set("recs", recs);

        // discounts 折扣列表[productId 产品id  beginPeoples 开始人数 endPeoples 结束人数 discount 折扣 ]
        List<Record> discounts = new ArrayList<>();
        discounts.add(new Record().set("beginPeoples", "0").set("endPeoples", "99").set("discount", "9.9").set("isCurrent", "0"));
        discounts.add(new Record().set("beginPeoples", "100").set("endPeoples", "199").set("discount", "9.8").set("isCurrent", "0"));
        discounts.add(new Record().set("beginPeoples", "200").set("endPeoples", "499").set("discount", "9").set("isCurrent", "1"));
        discounts.add(new Record().set("beginPeoples", "500").set("endPeoples", "999").set("discount", "8").set("isCurrent", "0"));

        record.set("discounts", discounts);
        return record;
    }

    public Record getLuckyBurstById(String productId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductById", productId);
        Record record = Db.findFirst(sqlPara);

        sqlPara = Db.getSqlPara("shop.getProductImagesById", productId);
        List<Record> images = Db.find(sqlPara);
        for (Record u : images
        ) {
            doImgPath(u);
        }

        record.set("images", images);


        sqlPara = Db.getSqlPara("shop.getProductColorsById", productId);
        List<Record> colors = Db.find(sqlPara);
        record.set("colors", colors);


        sqlPara = Db.getSqlPara("shop.getProductSpecsById", productId);
        List<Record> specs = Db.find(sqlPara);
        record.set("specs", specs);


        sqlPara = Db.getSqlPara("shop.getProductSkusById", productId);
        List<Record> skus = Db.find(sqlPara);
        record.set("skus", skus);

        sqlPara = Db.getSqlPara("shop.getProductCommentById", productId);
        Record comment = Db.findFirst(sqlPara);

        if (null != comment) {
            images = Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId", comment.getStr("commentId")));
            for (Record u : images
            ) {
                doImgPath(u);
            }
            doImgPath(comment);
            comment.set("images", null);
            if (null != images && images.size() > 0) {
                comment.set("images", images);
            }

        }
        record.set("comment", comment);


        sqlPara = Db.getSqlPara("shop.getProductRecsById", productId);
        List<Record> recs = Db.find(sqlPara);
        for (Record u : recs
        ) {
            doImgPath(u);
        }
        record.set("recs", recs);
        List<Record> users = null;

        users = Db.find(Db.getSqlPara("shop.getUsersByProductId", productId));
        for (Record u : users
        ) {
            doFilePath(u);
            doName(u);
        }
        record.set("users", users);
        sqlPara = Db.getSqlPara("shop.getLuckyBurstLastStockById", productId);
        Record lastStock = Db.findFirst(sqlPara);
        if (lastStock != null) {
            record.set("discount", BigNumberFormat.dataFormat1(lastStock.get("activityPrice"), lastStock.get("integral")));
            record.set("activityPrice", lastStock.get("activityPrice"));
            record.set("lastStocks", lastStock.get("lastStocks"));
            record.set("currentTime", lastStock.get("currentTime"));
            record.set("beginDate", lastStock.get("beginDate"));
            record.set("endDate", lastStock.get("endDate"));


            record.set("currentHour", lastStock.get("currentHour"));
        }
        return record;
    }

    public Record getSeckillById(String productId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getProductById", productId);
        Record record = Db.findFirst(sqlPara);

        sqlPara = Db.getSqlPara("shop.getProductImagesById", productId);
        List<Record> images = Db.find(sqlPara);
        for (Record u : images
        ) {
            doImgPath(u);
        }

        record.set("images", images);


        sqlPara = Db.getSqlPara("shop.getProductColorsById", productId);
        List<Record> colors = Db.find(sqlPara);
        record.set("colors", colors);


        sqlPara = Db.getSqlPara("shop.getProductSpecsById", productId);
        List<Record> specs = Db.find(sqlPara);
        record.set("specs", specs);


        sqlPara = Db.getSqlPara("shop.getProductSkusById", productId);
        List<Record> skus = Db.find(sqlPara);
        record.set("skus", skus);

        sqlPara = Db.getSqlPara("shop.getProductCommentById", productId);
        Record comment = Db.findFirst(sqlPara);

        if (null != comment) {
            images = Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId", comment.getStr("commentId")));
            for (Record u : images
            ) {
                doImgPath(u);
            }
            doImgPath(comment);
            comment.set("images", null);
            if (null != images && images.size() > 0) {
                comment.set("images", images);
            }

        }
        record.set("comment", comment);


        sqlPara = Db.getSqlPara("shop.getProductRecsById", productId);
        List<Record> recs = Db.find(sqlPara);
        for (Record u : recs
        ) {
            doImgPath(u);
        }
        record.set("recs", recs);
        List<Record> users = null;

        users = Db.find(Db.getSqlPara("shop.getUsersByProductId", productId));
        for (Record u : users
        ) {
            doFilePath(u);
            doName(u);
        }
        record.set("users", users);
        sqlPara = Db.getSqlPara("shop.getSeckillLastStockById", productId);
        Record lastStock = Db.findFirst(sqlPara);
        if (lastStock != null) {
            record.set("discount", BigNumberFormat.dataFormat1(lastStock.get("activityPrice"), lastStock.get("integral")));
            record.set("activityPrice", lastStock.get("activityPrice"));
            record.set("lastStocks", lastStock.get("lastStocks"));
            record.set("currentTime", lastStock.get("currentTime"));
            record.set("beginDate", lastStock.get("beginDate"));
            record.set("endDate", lastStock.get("endDate"));
            record.set("currentHour", lastStock.get("currentHour"));
        }


        return record;
    }

    public Record getMediaProductById(String productId, String provinceId) {

        SqlPara sqlPara = Db.getSqlPara("shop.getMediaProductById", productId);
        Record record = Db.findFirst(sqlPara);
        if (null == record) {
            return new Record();
        }
        record.set("images", null);
        record.set("skus", null);
        record.set("user", null);
        record.set("freight", 0);
        if (record != null) {
            sqlPara = Db.getSqlPara("shop.getMediaProductImagesById", productId);
            List<Record> images = Db.find(sqlPara);
            for (Record u : images
            ) {
                doImgPath(u);
            }
            record.set("images", images);

            sqlPara = Db.getSqlPara("shop.getMediaProductSkusById", productId);
            List<Record> skus = Db.find(sqlPara);
            record.set("skus", skus);
            if (!"0".equals("freightId")) {

                record.set("freight", countFreightIntegral(productId, provinceId));
            }
            Record user = Db.findFirst(Db.getSqlPara("user.getUserHeadImgInfo", record.getStr("mediaUserId")));
            doImgPath(user);
            user.set("loginTime", DateUtil.CalculateTime(user.getStr("loginTime")));
            record.set("user", user);
        }
        return record;

    }

    private int countFreightIntegral(String productId, String provinceId) {
        if (StrKit.isBlank(provinceId)) {
            return 0;
        }
        int number = 1;
        SqlPara sqlPara = Db.getSqlPara("mediaShop.selectProductFreight", productId);
        Record freightId = Db.findFirst(sqlPara);
        if (freightId == null || "0".equals(freightId.getStr("freightId"))) {
            return 0;
        }
        //子运费
        Record aeraFreight = Db.findFirst(Db.getSqlPara("mediaShop.selectAreaFreightByFreightId", freightId.getStr("freightId"), provinceId));
        if (aeraFreight != null) {
            return doNumberPrice(number, aeraFreight);
        } else {
            //主运费
            aeraFreight = Db.findFirst(Db.getSqlPara("mediaShop.selectFreightById", freightId.getStr("freightId")));
            if ("0".equals(aeraFreight.getStr("isExcept"))) {
                return doNumberPrice(number, aeraFreight);
            } else {
                if (aeraFreight.getStr("provinceId").contains(provinceId)) {
                    return 0;
                } else {
                    return doNumberPrice(number, aeraFreight);

                }
            }

        }

    }

    private int doNumberPrice(int num, Record aeraFreight) {
        int piece = aeraFreight.getInt("piece");
        int money = aeraFreight.getInt("money");
        int addPiece = aeraFreight.getInt("addPiece");
        int addMoney = aeraFreight.getInt("addMoney");
        int i = 0;
        if (num <= piece) {
            i = money;
        } else {
            i = money + ((int) ((num - piece) / addPiece) * addMoney);
        }

        return i;
    }

    public Record getMediaProductByProductId(String productId, String loginUid) {

        SqlPara sqlPara = Db.getSqlPara("shop.getMediaProductByProductId", productId, loginUid);
        Record record = Db.findFirst(sqlPara);
        if (null == record) {
            return new Record();
        }
        record.set("images", null);
        if ("0".equals(record.getStr("freightId"))) {
            record.set("freightName", "包邮");
        }
        if (record != null) {
            sqlPara = Db.getSqlPara("shop.getMediaProductImagesById", productId);
            List<Record> images = Db.find(sqlPara);
            for (Record u : images
            ) {
                doImgPath(u);
            }
            record.set("images", images);

            sqlPara = Db.getSqlPara("shop.getMediaProductSkusById", productId);
            List<Record> skus = Db.find(sqlPara);
            record.set("skus", skus);

        }
        return record;
    }

    @Before(Tx.class)
    public void deleteProductByImgId(String loginUid, String imgId) {
        String[] imgs = imgId.split(",");
        for (String s : imgs
        ) {
            Db.update(Db.getSqlPara("shop.deleteProductByImgId", s));
        }

    }

    @Before(Tx.class)
    public void deleteSku(String loginUid, String skuId) {
        String[] imgs = skuId.split(",");
        for (String s : imgs
        ) {
            Db.update(Db.getSqlPara("shop.deleteSku", s));
        }
    }

    @Before(Tx.class)
    public void deleteProduct(String loginUid, String productId) {
        Db.update(Db.getSqlPara("shop.deleteProduct", loginUid, productId));
    }

    public List<Record> getHotsShops() {
        List<Record> sc = Db.find(Db.getSqlPara("shop.getRecShopCategory"));

        for (Record r : sc
        ) {
            List<Record> list = Db.find(Db.getSqlPara("shop.getHotsShops", r.getStr("categoryId")));
            r.set("shops", list);
            for (Record r2 : list
            ) {
                doImgPath(r2);
            }
            doImgPath(r);
        }

        return sc;

    }

    public List<Record> getSelectedProducts() {
        List<Record> list = Db.find(Db.getSqlPara("shop.getSelectedProducts"));
        for (Record r : list
        ) {
            doImgPath(r);
        }
        return list;
    }

    public List<Record> getluckyVoucher() {
        List<Record> r = Db.find(Db.getSqlPara("shop.getluckyVoucher"));
        int currentHour = 0, beginDate = 0, endDate = 0;
        for (Record r1 : r
        ) {
            currentHour = r1.getInt("currentHour");
            beginDate = r1.getInt("beginDate");
            endDate = r1.getInt("endDate");
            r1.set("sortDate", currentHour - beginDate);
            if (currentHour >= beginDate && currentHour < endDate) {
                r1.set("sortDate", 0);
            } else {
                if (currentHour - beginDate > 0) {
                    r1.set("sortDate", currentHour - beginDate - 24);
                }
            }
            doImgPath(r1);
        }
        return r;
    }

    public Record getCouponsByProductId(String loginUid, String productId) {
        Kv cond = new Kv();
        cond.set("productId", productId);
        cond.set("loginUid", loginUid);

        Record record = new Record();
        SqlPara sqlPara = Db.getSqlPara("shop.getCouponsByProductId", cond);
        List<Record> coupons = Db.find(sqlPara);
        for (Record r : coupons
        ) {
            r.set("isHas", 0);
            if (StrKit.notBlank(r.getStr("has"))) {
                r.set("isHas", 1);
            }
            doImgPath(r);
        }
        record.set("coupons", coupons);

        sqlPara = Db.getSqlPara("shop.getRemCouponsByProductId", cond);
        List<Record> recs = Db.find(sqlPara);
        for (Record r : recs
        ) {
            doImgPath(r);
        }
        record.set("recs", recs);
        return record;
    }

    public Record getCouponByCouponId(String couponId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getCouponByCouponId", couponId);
        Record record = Db.findFirst(sqlPara);
        doImgPath(record);
        if (StrKit.isBlank(record.getStr("productId"))) {
            record.set("coupons", "0");
            return record;
        }

        List<Record> imgs = Db.find(Db.getSqlPara("shop.getProductImagesById", record.getStr("productId")));
        for (Record r : imgs) {
            doImgPath(r);
        }
        record.set("imgs", imgs);
        return record;
    }

    public Record getNewUserVipCouponByCouponId(String couponId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getNewUserVipCouponByCouponId", couponId);
        Record record = Db.findFirst(sqlPara);
        doImgPath(record);
        List<Record> imgs = Db.find(Db.getSqlPara("shop.getProductImagesById", record.getStr("productId")));
        for (Record r : imgs
        ) {
            doImgPath(r);
        }
        record.set("imgs", imgs);
        return record;
    }

    public Record getCollageByCouponId(String couponId) {
        SqlPara sqlPara = Db.getSqlPara("shop.getCollageByCouponId", couponId);
        Record record = Db.findFirst(sqlPara);
        doImgPath(record);
        List<Record> imgs = Db.find(Db.getSqlPara("shop.getProductImagesById", record.getStr("productId")));
        for (Record r : imgs
        ) {
            doImgPath(r);
        }

        record.set("imgs", imgs);
        return record;
    }

    public Record shopSearchProductHistory(String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("shop.hotsearch");
        List<Record> hots = Db.find(sqlPara);
        Record r = new Record();
        r.set("hosts", hots);
        r.set("historys", null);
        if (StrKit.notBlank(loginUid)) {
            sqlPara = Db.getSqlPara("shop.searchHistory", loginUid);
            List<Record> historys = Db.find(sqlPara);
            r.set("historys", historys);
        }

        return r;
    }

    @Before(Tx.class)
    public void deleteSearchHistory(String loginUid, String historyId) {
        Db.update(Db.getSqlPara("shop.deleteSearchHistory", historyId, loginUid));
    }

    @Before(Tx.class)
    public void emptySearchHistory(String loginUid) {
        Db.update(Db.getSqlPara("shop.emptySearchHistory", loginUid));
    }

    public List<Record> searchProduct(String loginUid, String name) {
        return Db.find(Db.getSqlPara("shop.searchProduct", name));
    }

    public Page<Record> searchProductList(String title, int pageNumber) {


        SqlPara sqlPara = Db.getSqlPara("shop.searchProductList", title);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            //doFilePath(r);
            doImgPath(r);
        }
        return records;
    }

    @Before(Tx.class)
    public void saveSearch(String loginUid, String title) {
        Record record = Db.findFirst(Db.getSqlPara("shop.searchHistoryByName", loginUid, title));
        if (record == null) {
            Db.update(Db.getSqlPara("shop.saveSearch", IdGen.uuid(), loginUid, title));

        } else {
            Db.update(Db.getSqlPara("shop.updateSearch", loginUid, title));
        }
        record = Db.findFirst(Db.getSqlPara("shop.searchHotHistoryByName", title));
        if (record == null) {
            Db.update(Db.getSqlPara("shop.savehotSearch", IdGen.uuid(), loginUid, title));
        } else {
            Db.update(Db.getSqlPara("shop.updatehotSearch", title));
        }


    }

    public Record getCollageByOrderNumber(String orderNumber) {
        Record record = Db.findFirst(Db.getSqlPara("mediaShop.getCollageByOrderNumber", orderNumber));
        doImgPath(record);
        int i = 0;
        List<Record> list = Db.find(Db.getSqlPara("mediaShop.getCollageByOrderNumberDetail", orderNumber));
        for (Record r : list
        ) {
            doImgPath(r);
        }
        i = list.size();
        record.set("list", list);
        record.set("lastNum", record.getInt("num") - i);
        return record;
    }

    public List<Record> getMyCouponsByProductId(String loginUid, String productId) {
        Kv cond = new Kv();
        cond.set("productId", productId);
        cond.set("loginUid", loginUid);
        SqlPara sqlPara = Db.getSqlPara("shop.getMyCouponsByProductId", cond);
        List<Record> coupons = Db.find(sqlPara);
        for (Record r : coupons
        ) {

            doImgPath(r);
        }
        return coupons;
    }

    public Page<Record> bottomProductList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("shop.bottomProductList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            //doFilePath(r);
            doImgPath(r);
        }
        return records;
    }
}
